package com.springexample.demo.component;

/**
 * 异常定义
 */
public enum  ExceptionEnum {
    IllegalStateException(500,IllegalStateException.class,"非法状态异常"),
    ArithmeticException(501,ArithmeticException.class,"算术异常"),
    ParamNullException(502, com.springexample.demo.exception.ParamNullException.class,"参数为空异常"),
    UnknowException(503,Exception.class,"未知异常");

    private final int code;
    private final Class<? extends Exception> exceptionClass;
    private final String message;

    ExceptionEnum(int code, Class<? extends Exception> exceptionClass, String message){
        this.code = code;
        this.exceptionClass = exceptionClass;
        this.message = message;
    }

    public int getCode() {
        return code;
    }

    public Class<? extends Exception> getExceptionClass() {
        return exceptionClass;
    }

    public String getMessage() {
        return message;
    }

    public static ExceptionEnum getByException(Class<? extends Exception> exceptionClass){
        for (ExceptionEnum e:ExceptionEnum.values()){
            if(exceptionClass.equals(e.exceptionClass)){
                return e;
            }
        }
        return ExceptionEnum.UnknowException;
    }
}
